<template>
  <el-row class="home" :gutter="20">
    <el-col :span="8" style="margin-top: 20px">
      <el-card shadow="hover">
        <div class="user">
          <img src="../assets/user.jpg" alt="" />
          <div class="user-info">
            <p class="user-info-admin">admin</p>
            <p class="user-info-p">超级管理员</p>
          </div>
        </div>
        <div class="login-info">
          <p>上次登录时间：<span>2024-06-30</span></p>
          <p>上次登录地点：<span>北京</span></p>
        </div>
      </el-card>
      <el-card shadow="hover" class="user-table">
        <el-table :data="tableData">
          <el-table-column
            v-for="(value, key) in tableLabel"
            :key="key"
            :label="value"
            :prop="key"
          ></el-table-column>
        </el-table>
      </el-card>
    </el-col>
    <el-col :span="16" style="margin-top: 20px">
      <div class="num">
        <el-card
          :body-style="{ display: 'flex', padding: 0 }"
          v-for="item in countData"
          :key="item.name"
        >
          <component
            :is="item.icon"
            class="icons"
            :style="{ background: item.color }"
          ></component>
          <div class="detail">
            <p class="num">¥{{ item.value }}</p>
            <p class="txt">{{ item.name }}</p>
          </div>
        </el-card>
      </div>
      <el-card class="top-echart">
        <div ref="echart" style="height: 280px"></div>
      </el-card>
      <div class="graph">
        <el-card>
          <div ref="userEchart" style="height: 240px"></div>
        </el-card>
        <el-card>
          <div ref="videoEchart" style="height: 240px"></div>
        </el-card>
      </div>
    </el-col>
  </el-row>
</template>

<script setup>
import { ref, getCurrentInstance, onMounted, reactive } from "vue";
import * as echarts from "echarts";

const { proxy } = getCurrentInstance();
const tableData = ref();
const tableLabel = ref({
  name: "课程",
  todayBuy: "今日购买",
  monthBuy: "本月购买",
  totalBuy: "总购买",
});
const countData = ref()
const observer = ref(null);
const getTableData = async () => {
  const res = await proxy.$api.getTableData();
  tableData.value=res.tableData;
}
const getCountData = async () => {
  const res = await proxy.$api.getCountData();
  console.log(res);
  countData.value=res;
}
const getChartData = async () => {
  const { orderData, videoData, userData } = await proxy.$api.getChartData();

  xOptions.xAxis.data = orderData.date;
  xOptions.series = Object.keys(orderData.data[0]).map((val) => {
    return {
      name: val,
      data: orderData.data.map((item) => item[val]),
      type: "line",
    };
  });

  // 对第一个图表进行x轴 和 series 赋值
  const oneEchart = echarts.init(proxy.$refs["echart"]);
  oneEchart.setOption(xOptions);

  // 对第二个表格进行渲染
  xOptions.xAxis.data = userData.map((item) => item.date);
  xOptions.series = [
    {
      name: "新增用户",
      type: "bar", // 柱状图
      data: userData.map((item) => item.new),
    },
    {
      name: "活跃用户",
      type: "bar", // 折线图
      data: userData.map((item) => item.active),
    },
  ];

  const twoEchart = echarts.init(proxy.$refs["userEchart"]);
  twoEchart.setOption(xOptions);

  // 对第三个图表进行渲染
  pieOptions.series = [
    {
      data: videoData,
      type: "pie", // 饼图
    },
  ];

  const threeEchart = echarts.init(proxy.$refs["videoEchart"]);
  threeEchart.setOption(pieOptions);

  // 监听页面变化
  observer.value = new ResizeObserver(() => {
    oneEchart.resize();
    twoEchart.resize();
    threeEchart.resize();
  });
  
  if (proxy.$refs["echart"]) {
    observer.value.observe(proxy.$refs.echart);
  }
};
// 这个是折线图和柱状图 两个图表共用的公共配置
const xOptions = reactive({
  // 图例文字颜色
  textStyle: {
    color: "#333", // 设置文字颜色为白色
  },
  legend: {},
  grid: {
    left: "20%",
  },
  // 提示框
  tooltip: {
    trigger: "axis", // 坐标轴触发
  },
  xAxis: {
    type: "category", // 类目轴
    data: [],
    axisLine: {
      lineStyle: {
        color: "#17b3a3", // 轴线颜色
      },
    },
    axisLabel: {
      color: "#333", // 轴标签颜色
      interval: 0, // 显示所有标签
    },
  },
  yAxis: [
    {
      type: "value", // 值轴
      axisLine: {
        lineStyle: {
          color: "#17b3a3", // 轴线颜色
        },
      },
    },
  ],
  color: ["#2eb3a3", "#ff4500", "#ffd700", "#ffd700", "#ffd700", "#ffd700"],
  series: [],
});

const pieOptions = reactive({
  tooltip: {
    trigger: "item", // 数据项图形触发
  },
  legend: {},
  color: ["#2eb3a3", "#ff4500", "#ffd700", "#ffd700", "#ffd700", "#ffd700"],
  series: [{}],
});


onMounted(() => {
  getTableData();
  getCountData();
  getChartData();
});
</script>

<style lang="less" scoped>
.home {
  height: 100%;
  overflow: hidden;
  .user {
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
    margin-bottom: 20px;
    img {
      //   display: inline-block;
      width: 150px;
      height: 150px;
      border-radius: 50%;
      margin-right: 40px;
      overflow: hidden;
      margin-bottom: 10px;
    }
    .user-info {
      p {
        line-height: 40px;
      }
      .user-info-p {
        color: #999;
      }
      .user-info-admin {
        font-size: 35px;
      }
    }
  }
  .login-info {
    p {
      line-height: 30px;
      font-size: 14px;
      color: #999;
      span {
        color: #666;
        margin-left: 60px;
      }
    }
  }
  .user-table {
    margin-top: 20px;
  }
  .num {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    .el-card {
      width: 32%;
      margin-bottom: 20px;
    }
    .icons {
      width: 80px;
      height: 80px;
      font-size: 30px;
      text-align: center;
      line-height: 80px;
      color: #fff;
    }
    .detail {
      margin-left: 15px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      .num {
        font-size: 30px;

        margin-bottom: 10px;
      }
      .txt {
        color: #999;
        font-size: 15px;
        text-align: center;
      }
    }
  }
  .graph {
    margin-top: 20px;
    display: flex;
    justify-content: space-between;
    .el-card {
      width: 48%;
      height: 260px;
    }
  }
}
</style>
